How It Works -- Being Sneaky How It Works -- Being Sneaky:

The problem that Features of Light and Darkness faces is that spectra of light come in vast variety, with a different intensity of light at every wavelength, whereas the liquid-crystal display (LCD) screens of today's portable devices only emit combinations of three specific spectra of light: One is what you get when you tell the display to emit red light, the second is what you get when you ask for green light, and the third is for blue light. There is no way you can use just those three spectra, in combination, to make up all the possible spectra of light that you might see in nature, or that you might wish to display in Features of Light and Darkness. An LCD display is a three-parameter system; all you get to tell it is how much of its kind of red light (let's call that R), how much of its kind of green light (G), and how much of its kind of blue light (B) to emit. The task of creating an arbitrary spectrum is mathematically impossible! What is poor Features of Light and Darkness supposed to do?

Features of Light and Darkness gets around this problem by being sneaky. The sneakiness is made possible because the human eye is also a three-parameter system: It has only three kinds of color-detecting cells in its retina. (At least that is how it works in bright light, and I have more to say about dimmer conditions elsewhere herein.)

The three parameters that characterize how the eye sees color are the responses of its red cone cells, green cone cells, and blue cone cells to the light in question. Let's call those responses r, g, and b, respectively, and let's also use those same letters to keep track of which cone cell is which. Note that I am using capital letters for the kinds of light spectra emitted by the hardware that comprises the LCD display of your portable device, and lower-case letters to describe the physiology of the human eye.

Since the eye is a three-parameter system, it follows -- and it should be intuitively clear -- that the eye cannot tell the difference between all of the possible kinds of light that it might see. Any two light sources that stimulate the r, g and b cone cells the same way will look the same, even if the patterns of light that you see when you spread the spectra out with a prism -- or look at them as an image in Features of Light and Darkness -- look very different.

The fact that there are many huge groups of different spectra that all look the same to the eye is the basis for how Features of Light and Darkness can be sneaky. When Features of Light and Darkness encounters a spectrum that it cannot display, it tries to find a different spectrum that looks the same to the eye, that it can display. If it can find such a spectrum, it will substitute that one for the original, and display it instead. You -- the user of the program -- won't be able to tell the difference.

Some times, Features of Light and Darkness can find a suitable substitute spectrum, and sometimes it cannot. When it can find one, Features of Light and Darkness works as intended. When it cannot, Features of Light and Darkness presents a little panel explaining that there is a problem, with some details about it in the fine print, and then does the best it can, which may or may not be good enough.

In a little more detail, what Features of Light and Darkness does is ask the question, "Is there a combination of R, G, and B lights that will cause the eye to have the same r, g, and b responses as this spectrum that I am unable to display?" The reason why the answer is not always "yes" is that sometimes the mathematics works out to require a negative amount of one or more of R, G, or B: Unfortunately, your LCD display cannot produce negative amounts of light -- it cannot subtract off some G light, for example, when there isn't any to start with. In situations like that, Features of Light and Darkness is out of luck.

I would like to use a little mathematics to explain things more carefully here; skim ahead if you wish.

Consider a 3x3 matrix whose first column, from top to bottom, is the convolution of the R spectrum with the spectral sensitivity of the r cone cells, the convolution of the R spectrum with the spectral sensitivity of the g cone cells, and the convolution of the R spectrum with the spectral sensitivity of the b cone cells. Let the second and third columns of this matrix be similar convolutions, only prepared using the G and B spectra respectively. Call this matrix E. If we post-multiply E by a vector whose components are the amounts of R, G, and B light that an LCD screen is emitting, the vector that results has components that are the responses of the r, g, and b cone cells.

What good does the matrix E do us? Well, suppose we have a spectrum that we wish to display. Let us prepare a vector by convolving it with the spectral sensitivities of the r, g, and b cone cells respectively; the components will be the responses of those cells to the light in question. If we postmultiply the inverse matrix of E (and it does turn out that E is invertible) by that vector, we will get a vector whose values are the amounts of R, G, and B that will stimulate the cone cells in the same way as the spectrum we started with. Those values are the ones we use to be sneaky; they combine to create the substitute spectrum that we wish to display instead of the original one.

The only problem is, that as I indicated earlier, the values of R, G, and B produced thereby are not guaranteed to be positive, and Features of Light and Darkness has no means to make your LCD screen emit negative amounts of light. The approach the app takes is that when an amount comes out negative, it always sets the actual corresponding value of R, G, or B to zero -- that is the best it can do -- but it does not bother to warn you if the size of the negative amount is small. (Strictly, it warns you of the problem only if the negative amount of light requested exceeds three percent of the largest positive amount of light requested; I set that value empirically based on what looked good.)

When it comes to twilight conditions, there is another problem. In twilight, the eye is a four-parameter system -- the rod cells are active as well as the cones -- but there still are only three kinds of light emitted by the LCD display, so there is no fancy way to show the response of the rods. Fortunately, a simple sneaky way will do. When the rods are active, I calculate their response to the light in question by convolving the spectrum of the light with the spectral response of the rod cells. Then I increase the G and B values by that amount; the effect is that areas to which the rods are particularly sensitive show up as brighter than they would otherwise be, and with a blue-green tint. That will probably do. And when the light is even darker, when the cone cells are turned off, then I simply use the rod cell response to adjust the intensity of gray in the image.